package com.green.business.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.green.business.domain.Goods;
import com.green.business.domain.Inport;
import com.green.business.domain.Provider;
import com.green.business.domain.Sales;
import com.green.business.mapper.GoodsMapper;
import com.green.business.mapper.InportMapper;
import com.green.business.mapper.ProviderMapper;
import com.green.business.mapper.SalesMapper;
import com.green.business.service.GoodsService;
import com.green.business.service.InportService;
import com.green.business.service.ReportsService;
import com.green.business.service.SalesService;

import com.green.business.vo.ReportsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class ReportsServiceImpl implements ReportsService {

    @Autowired
    private InportMapper inportMapper;
    @Autowired
    private InportService inportService;

    @Autowired
    private SalesService salesService;
    @Autowired
    private SalesMapper salesMapper;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ProviderMapper providerMapper;
    private Double sum ;
    private Double profit_sum = 0.0;
    private Integer goodsnum = 0;
    private Double marketsum=0.0;


    @Override
    public Double queryInports(ReportsVo reportsVo) {
        sum = 0.0;
        QueryWrapper<Inport> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "inporttime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "inporttime", reportsVo.getEndTime());

        List<Inport> inportList = this.inportMapper.selectList(queryWrapper);
        for (Inport record : inportList) {
            if (null != record.getId()){
                sum += record.getNumber() * record.getInportprice();
            }
        }
        reportsVo.setInportsum(sum);
        return reportsVo.getInportsum();
    }

    @Override
    public Double querySales(ReportsVo reportsVo) {
        sum = 0.0;
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        List<Sales> salesList = this.salesMapper.selectList(queryWrapper);
        for (Sales record : salesList) {
            if (null != record.getId()) {
                if (record.getCustomerid()==0) {
                    sum += record.getNumber() * record.getPrice();
                }
                else {
                    sum+=record.getNumber()*record.getMemberprice();
                }
            }

        }
        reportsVo.setSalessum(sum);
        return reportsVo.getSalessum();
    }


    @Override
    public List<Map.Entry<String, Integer>> querySalableGoods(ReportsVo reportsVo) {
        //自定义map函数，当键值相同时值相加。
        class MyHashMap<K> extends HashMap<K,Integer> {
            /**
             * 使用HashMap中containsKey判断key是否已经存在
             * @param key
             * @param value
             * @return
             */
            @Override
            public Integer put(K key, Integer value) {
                Integer newV = value;
                if (containsKey(key)) {
                    Integer oldV = get(key);
                    newV = oldV +newV;
                }
                return super.put(key, newV);
            }
        }
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        List<Sales> salesList1 = this.salesMapper.selectList(queryWrapper);
        MyHashMap<String> countgoods=new MyHashMap<>();
        for (Sales record : salesList1) {
            if (null != record.getId()) {
                //通过销售表的商品id查询商品名称,商品名称出现次数最多即为畅销商品
                QueryWrapper<Goods> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq(record.getGoodsid() != null, "id", record.getGoodsid());
                Goods goods = this.goodsMapper.selectOne(queryWrapper1);
                record.getNumber();//销售数量
                countgoods.put(goods.getGoodsname(), record.getNumber());
            }
        }
        //对商品销售量进行排序(降序)
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(countgoods.entrySet()); //转换为list
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        reportsVo.setSalablegoods(list);
            return reportsVo.getSalablegoods();
        }



    @Override
    public Integer queryGoodsnum(ReportsVo reportsVo) {
        goodsnum = 0;
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        List<Goods> goodsList = this.goodsMapper.selectList(queryWrapper);
        for (Goods record : goodsList) {
            if (null != record.getId()) {
                goodsnum += record.getNumber();
            }
        }
        reportsVo.setGoodsnum(goodsnum);
        return reportsVo.getGoodsnum();
    }

    @Override
    public Double queryMarketsum(ReportsVo reportsVo) {
        marketsum=0.0;
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        List<Goods> goodsList=this.goodsMapper.selectList(queryWrapper);
        for(Goods record:goodsList){
            if(null!=record.getId()){
                marketsum+=record.getNumber()*record.getPrice();
            }
            reportsVo.setMarketsum(marketsum);
        }
        return reportsVo.getMarketsum();
    }

    @Override
    public List<Map.Entry<String, Integer>> querySalableGoodsType(ReportsVo reportsVo) {
        //自定义map函数，当键值相同时值相加。
        class MyHashMap<K> extends HashMap<K,Integer> {
            /**
             * 使用HashMap中containsKey判断key是否已经存在
             * @param key
             * @param value
             * @return
             */
            @Override
            public Integer put(K key, Integer value) {
                Integer newV = value;
                if (containsKey(key)) {
                    Integer oldV = get(key);
                    newV = oldV +newV;
                }
                return super.put(key, newV);
            }
        }
        QueryWrapper<Sales> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge(reportsVo.getStartTime() != null, "salestime", reportsVo.getStartTime());
        queryWrapper.le(reportsVo.getEndTime() != null, "salestime", reportsVo.getEndTime());
        List<Sales> salesList1 = this.salesMapper.selectList(queryWrapper);
        MyHashMap<String> countgoodstype=new MyHashMap<>();
        for (Sales record : salesList1) {
            if (null != record.getId()) {
                //通过销售表的商品id查询供应商名称,供应商名称出现次数最多即为畅销商品类型
                QueryWrapper<Goods> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq(record.getGoodsid() != null, "id", record.getGoodsid());
                Goods goods = this.goodsMapper.selectOne(queryWrapper1);
                record.getNumber();//销售数量
                QueryWrapper<Provider> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq(goods.getId()!=null,"id",goods.getProviderid());
                Provider provider=this.providerMapper.selectOne(queryWrapper2);
                countgoodstype.put(provider.getProvidername(), record.getNumber());
            }
        }
        //对商品销售量进行排序(降序)
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(countgoodstype.entrySet()); //转换为list
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        reportsVo.setSalablegoodsType(list);
        return reportsVo.getSalablegoodsType();

    }
}





